package com.boc.langxiang.ui.activity.shop;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.entity.bean.ProductDetailInfo;
import com.boc.langxiang.ui.activity.BaseActivity;
import com.boc.langxiang.ui.fragment.shop.PhotoFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PhotoDetailActivity extends BaseActivity {

    @BindView(R.id.photo_pager)
    ViewPager photoPager;
    @BindView(R.id.content_tv)
    TextView contentTv;
    @BindView(R.id.index_layout)
    LinearLayout indexLayout;
    @BindView(R.id.bottom_layout)
    LinearLayout bottomLayout;

    private PagerAdapter mPagerAdapter;

    private int mPosition;

    private ImageView mImageView;

    private ImageView[] mImageViews;

    private ProductDetailInfo mProductDetailInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_detail);
        ButterKnife.bind(this);
        if (getIntent() != null && getIntent().getParcelableExtra("info") != null) {
            mProductDetailInfo = getIntent().getParcelableExtra("info");
        }

        if (getIntent() != null) {
            mPosition = getIntent().getIntExtra("position", 0);
        }
        initViews();
    }

    private void initViews() {
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), new ArrayList<Fragment>());
        photoPager.setAdapter(mPagerAdapter);
        updateUI();
    }

    private void updateUI() {
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < mProductDetailInfo.getPhoto().size(); i++) {
            String url = mProductDetailInfo.getPhoto().get(i).getUrl();
            PhotoFragment fragment = PhotoFragment.getInstance(url);
            fragment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            fragments.add(fragment);
        }
        mPagerAdapter.setFragmentList(fragments);
        mPagerAdapter.notifyDataSetChanged();

        mImageViews = new ImageView[fragments.size()];
        for (int i = 0; i < fragments.size(); i++) {
            mImageView = new ImageView(mContext);
            mImageView.setAdjustViewBounds(true);
            mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mImageViews[i] = mImageView;
            if (i == 0) {
                mImageViews[i].setImageResource(R.drawable.icon_point_white);
            } else {
                mImageViews[i].setImageResource(R.drawable.icon_point_normal);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.rightMargin = 12;
            params.leftMargin = 12;
            mImageView.setLayoutParams(params);
            indexLayout.addView(mImageView);
        }

        photoPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < mImageViews.length; i++) {
                    mImageViews[i].setImageResource(R.drawable.icon_point_normal);
                }

                mImageViews[position].setImageResource(R.drawable.icon_point_white);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        photoPager.setCurrentItem(mPosition);

        contentTv.setText(mProductDetailInfo.getContent());

    }

    private class PagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragmentList;

        public PagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        public List<Fragment> getFragmentList() {
            return fragmentList;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }

        public void setFragmentList(List<Fragment> fragmentList) {
            this.fragmentList = fragmentList;
        }
    }
}
